
/**
 * 
 * @since 2012-01-06
 * @description CcayTree 核心脚本库
 */


/** ----------------------------------------- Ccay.Tree 命名空间的实现----------------- ------------------------- */ 
(function(){
		//default setting of core
		var  defaultIdKey = "defaultId",defaultPidKey = "defaultPid",defaultName = "defaultName",defaultChildren = "defaultChildren",defaultTitle = "defaultTitle",
		tree_setting = {
			data: {
				key: {
					name: "name",
					children: "children",
					title: ""
				},
				simpleData: {
					enable: false,
					idKey: "id",
					pIdKey: "pId",
					rootPId: null
				},
				keep: {
					parent: false,
					leaf: false
				}
			}
		};
	    Ccay.tree = {
	    	/**
	    	 * obj 显示树对象
	    	 * zSetting 参数设置
	    	 * jsonObject
	    	 * 对象数组
	    	 * intricateDate 数据模型 嵌套复杂的 多个vo的(true) 还是 树结构vo name和其他的都在vo中存在(false)
	    	 * attr 需要绘制的属性值attr["a","b","c"];
	    	 */
		    init: function(obj, zSetting, objectData,intricateDate,attr){
		    	var jsonObject;
		    	if(intricateDate){
					jsonObject = this.toJsonIntricate(zSetting,objectData,attr);
		    	}else{
		    		
		    		jsonObject = this.toJsonSimple(zSetting,objectData,attr);
		    	}
				return $.fn.zTree.init(obj, zSetting,jsonObject);
		    },
		    /**
		     * 把对象数据转化为需要的json数据返回
		     */
	   		toJsonIntricate: function(zSetting,objectData,attr){
	   			var setting = $.fn.zTree._z.tools.clone(tree_setting);
				$.extend(true, setting, zSetting);
				var userKey = {
					 key : setting.data.simpleData.idKey.split(","),
					 parentKey : setting.data.simpleData.pIdKey.split(","),
					 childrenKey : setting.data.key.children.split(","),
					 nameKey : setting.data.key.name.split(","),
					 titleKey : setting.data.key.title.split(",")
				}
				/**
				 * 如果
				 */
				zSetting.data.simpleData.idKey = defaultIdKey;
				zSetting.data.simpleData.pIdKey = defaultPidKey;
				zSetting.data.key.children = defaultChildren;
				zSetting.data.key.name = defaultName;
				zSetting.data.key.title = defaultTitle;
				//
				if (setting.data.simpleData.enable) {	
					
				}else{
					if ($.fn.zTree._z.tools.isArray(objectData)){
						  return this.getNewObjectData(objectData,userKey,attr);
					}else{
						return objectData;
					}
				}
				
	   		},
	   		 /**
		     * 把简单数据对象数据转化为需要的json数据返回
		     */
	   		toJsonSimple: function(zSetting,objectData,attr){
	   			if(attr == null || attr.length <1){
	   			   return objectData
	   			}else{
	   			 for (var i=0;i<objectData.length;i++) {
	   					objectData[i] = this.getNewSimpleObjectData(oldData[i],attr);
	   			 }
	   			  return objectData;
	   			   
	   			}
	   		},
	   		/**
	   		 * 获取每个精简后和替换完成特殊显示的object对象
	   		 */
	   		getNewObjectData:function(oldData,userKey,attr){
	   		 var newObjectData = [];
	   		 var value;
	   		 	for (var i=0;i<oldData.length;i++) {
	   		 		//精简对象
	   		 		oldData[i] = this.getNewSimpleObjectData(oldData[i],attr);
	   		 		
	   		 		//替换一些对象变量
		   		 	value = this.getObjectDataByKeys(oldData[i],userKey.key);
		   		 	oldData[i][defaultIdKey] = value;
		   		 	value = this.getObjectDataByKeys(oldData[i],userKey.parentKey);
		   		 	oldData[i][defaultPidKey] = value;
		   		 	value = this.getObjectDataByKeys(oldData[i],userKey.nameKey);
		   		 	oldData[i][defaultName] = value;
		   		 	value = this.getObjectDataByKeys(oldData[i],userKey.titleKey);
		   		 	oldData[i][defaultTitle] = value;
		   		 	value = this.getObjectDataByKeys(oldData[i],userKey.childrenKey);
		   		 	/**
		   		 	 * 如果有子  迭代子做同样的操作
		   		 	 */
		   		 	if(value != null){
		   		 		oldData[i][defaultChildren] = this.getNewObjectData(value,userKey,attr);
		   		 	}
		   		 	newObjectData.push(oldData[i]);
	   		 	}
	   		 	return newObjectData;
	   		},
	   		/**
	   		 * 获取每个精简后的sub objects对象
	   		 */
	   		getNewSimpleObjectData:function(objectData,attr){
	   			 /**
		   		  * 如果需要替换显示key值 那么迭代 否则直接返回
		   		  */
		   		 if(attr != null && attr.length>1){
		   		 	var newSimpleData = {};
		   		 	for(var i=0;i<attr.length;i++){
		   		 		if(attr[i].indexOf(".")>-1){
		   		 			var attrKey = attr[i].split(".");
			   			    if(attrKey.length>2){
			   			    	alert("afford two level[" + attr[i] +"].");
			   			    }
			   			    if(objectData[attrKey[0]] != null && objectData[attrKey[0]][attrKey[1]]!=null){
			   			    	if(newSimpleData[attrKey[0]] == null){
			   			    		newSimpleData[attrKey[0]]={};
			   			    	}
			   			    	newSimpleData[attrKey[0]][attrKey[1]] = objectData[attrKey[0]][attrKey[1]]; 
			   			    }
		   		 		}else{
			   		 		if(objectData[attr[i]]!=null){
			   		 			newSimpleData[attr[i]]=objectData[attr[i]];
			   		 		}
		   		 		}
		   		 	}
		   		 	return newSimpleData;
		   		 }
		   		 //不需要精简
		   		 return objectData;
	   		},
	   		/**
	   		 * 获取设置的key值 看是否存在
	   		 */
	   		getObjectDataByKeys:function(oldData,keys){
	   			for(var i = 0;i<keys.length;i++){
	   			 //看是否获取对象属性的值
	   			 if(keys[i].indexOf(".")>-1){
	   			    var attrArray = keys[i].split(".");
	   			    if(attrArray.length>2){
	   			    	alert("afford two level[" + keys[i] +"].");
	   			    }
	   			     if(oldData[attrArray[0]]!= null &&　oldData[attrArray[0]][attrArray[1]]!=null){
		   			 		return oldData[attrArray[0]][attrArray[1]];
		   			 }
	   			 }else{	
		   			 if(oldData[keys[i]] != null){
		   			 	return oldData[keys[i]];
		   			 }
	   			 } 
	   			}
	   			return '';
	   		},
	   		getZTreeObj:function(ccayTreeId){
	   			return $.fn.zTree.getZTreeObj(ccayTreeId);
	   		},
	   		/**
	   		 * 获取子选中的所有对象
	   		 */
	   		getSubCheckedNodes:function(treeObj,checked){
	   			var checkedNodes = treeObj.getCheckedNodes(checked);
	   			var subNodes = [];
	   			for(var i=0;i<checkedNodes.length;i++){
	   				if(!checkedNodes[i].isParent){
	   					subNodes.push(checkedNodes[i]);
	   				}
	   			}
	   			return subNodes;
	   		}
	   		
			
	   }
})();
